package com.lottery.material.aspect;

import com.lottery.material.exception.ResponseException;
import com.lottery.material.utils.RedisUtil;
import com.lottery.material.utils.StringUtil;
import com.lottery.material.vo.BaseBody;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 校验accessToken
 * @Author 焦阳
 */
@Component
@Aspect
@Slf4j
public class CheckLoginAspect {

    @Autowired
    RedisUtil redisUtil;

    @Pointcut("@annotation(com.lottery.material.aspect.CheckLogIn)")
    public void addPointCut() {

    }

    @Around("addPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        Object[] args = point.getArgs();
        if (args != null && args.length > 0) {
            for (int i = 0; i < args.length; i++) {
                Object arg = args[i];
                if(arg instanceof BaseBody){
                    String userId = ((BaseBody) arg).getUserId();
                    String accessToken = ((BaseBody) arg).getAccessToken();
                    Object hget = redisUtil.hget("portalUser", userId);
                    String s = String.valueOf(hget);
                    if(!accessToken.equals(s)){
                        throw new ResponseException(403,"登录过期，请重新登录！");
                    }
                }
            }
        }else{
            throw new ResponseException(400,"参数错误！");
        }
        return point.proceed();
    }

}
